﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LPV_POS.In
{
    public class PrintCOM
    {
        Print_Type ePrint_Type;
        /// <summary>
        /// chỉ in 1 luc 1 máy in
        /// </summary>
        /// <param name="_ePrint_Type"></param>
        public PrintCOM(Print_Type _ePrint_Type)
        {
            ePrint_Type = _ePrint_Type;
        }

        private SerialPort_LPV COMport;

        #region "   local Property"
        string Print_Align_Center = ((char)(27)).ToString() + ((char)(97)).ToString() + ((char)(1)).ToString();
        string Print_Align_Left = ((char)(27)).ToString() + ((char)(97)).ToString() + '\0';

        string Print_Font_Double_Height = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(16)).ToString();
        string Print_Font_Normal = ((char)(27)).ToString() + ((char)(33)).ToString() + ((char)(1)).ToString();//_40CharPerRow

        string Print_BreakPage = ((char)(29)).ToString() + ((char)(86)).ToString() + ((char)(66)).ToString() + ((char)(1)).ToString();

        int iSpace1 = 32, iSpace2 = 7, iSpace3 = 17;   // = 56
        #endregion

        #region "   Enum"
        #region "   Enum Fonts"
        public enum Fonts
        {
            Font_Normal,
            Font_Double_Height
        }
        #endregion

        #region "   Enum aligns"
        public enum aligns
        {
            Align_Center,
            Align_Left
        }
        #endregion

        #region "   Enum Width"
        public enum Fills
        {
            Fills_Full,
            Fills1Row3,
            Fills2Row3
        }
        #endregion

        #region "   Enum Print_Type"
        public enum Print_Type
        {
            Print_BILL,
            Print_KitChen
        }
        #endregion
        #endregion

        #region "   All Main Print"
        #region "   1.Print_Start"
        /// <summary>
        /// Print_Start()
        ///     - Nếu Return TRUE, thì mới thực hiện tiếp Print_Content
        ///     - Ngược lại, Kết thúc.
        /// </summary>
        /// <returns></returns>
        public Boolean Print_Start()
        {
            try
            {
                if (ePrint_Type == Print_Type.Print_BILL)
                {
                    //1. Load thông tin máy in
                    //if (Variable.arrDM_MAYIN_HD == null)
                        new Variable().Get_DM_MAYIN_HD();

                    //2. Mở cổng máy in
                    //if (Variable.gsrlCOMBillport == null)
                        new Variable().SerialPort_Open();

                    //3.
                    int intLoop = 0; //So máy in
                    for (intLoop = 0; intLoop < Variable.arrDM_MAYIN_HD.Length; intLoop++)
                    {
                        //mở cổng COM
                        //if (Variable.arrDM_MAYIN_HD[intLoop]["KIND"] + "" == "0") //in cổng COM K80
                        {
                            COMport = Variable.gsrlCOMBillport[intLoop];
                            //if(!Variable.gsrlCOMBillport[intLoop].IsOpen)
                            //    Variable.gsrlCOMBillport[intLoop].Open();
                            return true;
                        }
                    }
                }
                else if (ePrint_Type == Print_Type.Print_KitChen)
                {
                    //1. Load thông tin máy in
                    //if (Variable.arrDM_MAYIN_BEP == null)
                    new Variable().Get_DM_MAYIN_BEP();//

                    //2. Mở cổng máy in
                    //if (Variable.gsrlCOMBillport == null)
                        new Variable().SerialPort_Open_KitChen();

                    //3.
                    int intLoop = 0; //So máy in
                    for (intLoop = 0; intLoop < Variable.arrDM_MAYIN_BEP.Length; intLoop++)
                    {
                        //mở cổng COM
                            COMport = Variable.gsrlCOMBillport[intLoop];
                            return true;
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return false;
        }
        #endregion

        #region "   2.Print_Content"
        public void Print_Content(aligns _align, Fonts _Font, string _Content1, string _Content2, string _Content3, string _Content4)
        {
            this.Write_Align(_align);

            this.Write_Font(_Font);
            string _Contents = this.str4Column(_Font, _Content1, _Content2, _Content3, _Content4);
            COMport.WriteLine(_Contents);
        }
        public void Print_Content(aligns _align, Fonts _Font, string _Content1, string _Content2, string _Content3, string _Content4, string space)
        {

            this.Write_Align(_align);

            this.Write_Font(_Font);
            string _Contents = this.str4Column(_Font, _Content1, _Content2, _Content3, _Content4, space);
            COMport.WriteLine(_Contents);
        }
        #endregion

        #region "   3.Print_Line"
        public void Print_Line(aligns _align, Fonts _Font, char chr, Fills _Fills)
        {
            //chr: "=", "-"
            this.Write_Align(_align);
            this.Write_Font(_Font);
            int ichar = 56;
            if (_Fills == Fills.Fills1Row3)
                ichar = ichar / 3;
            else if (_Fills == Fills.Fills2Row3)
                ichar = ichar * 2 / 3;

            COMport.WriteLine(new String(chr, ichar));
        }
        #endregion

        #region "   4.Print_Close"
        public void Print_Close()
        {
            COMport.WriteLine("\n");
            COMport.WriteLine(Print_BreakPage);
            if (ePrint_Type == Print_Type.Print_BILL)
                new Variable().SerialPort_Close();
            else if (ePrint_Type == Print_Type.Print_KitChen)
                new Variable().SerialPort_Close_KitChen();
        }
        #endregion
        #endregion

        #region "   strSetting"
        #region "   str4Column"
        string str4Column(Fonts _Font, string a, string b, string c, string d, string space)
        {
            if (space != "")
                return str4Column(_Font,a, b, c, d).Replace(" ", space);
            else
               return  str4Column(_Font,a, b, c, d);
        }
        string str4Column(Fonts _Font,string a, string b, string c, string d)
        {//iSpace1: 32; iSpace2:7; iSpace3: 17 = 
            cpublic ocpublic=  new cpublic();
            a = ocpublic.RemoveSign4VietnameseString(a);
            b = ocpublic.RemoveSign4VietnameseString(b);
            c = ocpublic.RemoveSign4VietnameseString(c);
            d = ocpublic.RemoveSign4VietnameseString(d);
            if (b == "" && c == "" && d != "")
                return this.str2Column(_Font,a, d);
            
            string str = "";
            str = a;
            str += new cpublic().Spacestring(iSpace1 - a.Length - b.Length);
            str += b;
            str += new cpublic().Spacestring(iSpace2 - c.Length);
            str += c;
            str += new cpublic().Spacestring(iSpace3 - d.Length);
            str += d;
            if (b == "" && c == "" && d == "")
                str = str.Trim();
            return str;
        }
        #endregion
        #region "   str2Column"
        string str2Column(Fonts _Font, string a, string b)
        {//i: 56
            int i = 56;
            if (_Font == Fonts.Font_Double_Height)
                i = 42;
            
            string str = "";
            str = a;
            str += new cpublic().Spacestring(i - a.Length - b.Length);
            str += b;
            return str;
        }
        #endregion

        #region "   Write_Align"
        void Write_Align(aligns _align)
        {
            string strAlign = Print_Align_Center;
            if (_align == aligns.Align_Left)
                strAlign = Print_Align_Left;
            COMport.Write(strAlign);
        }
        #endregion

        #region "   Write_Font"
        void Write_Font(Fonts _Font)
        {
            string strFont = Print_Font_Normal;
            if (_Font == Fonts.Font_Double_Height)
                strFont = Print_Font_Double_Height;
            COMport.Write(strFont);
            string s = "";
        }
        #endregion
        #endregion
    }
}
